<?php

class MateriaisController extends Site_Controller_Action
{   
    public $limitRegistros = 4;

    public function init()
    {
        
    }

    public function indexAction()
    {
        $this->view->headScript()->appendFile($this->_urlbase.'/public/media/js/view/materiais.js');
        $this->view->limitRegistros = $this->limitRegistros;
        
        $obj = new Ead_Model_DbTable_Catcurso();
        $this->view->filtro = $obj->fetchAll()->toArray();
        
        $obj = new Ead_Model_DbTable_Material();
        $result = $obj->fetchAll($obj->select()->where('MAT_STA = 1')->order(array('MAT_ORD ASC','MAT_NOM DESC'))->limit($this->limitRegistros,0));
        
        $this->view->result = $result; 
                
        $count = 0;
        $btn_mais = 1;
        $total = $obj->fetchAll('MAT_STA = 1');
        $count = count($total);
        if($count == 0){
            $btn_mais = 0;
        }else if($count <= $this->limitRegistros) {
            $btn_mais = 0;
        }
        
        $this->view->btn_mais = $btn_mais;
        
        $obj = new Ead_Model_DbTable_Catcurso();
        $row = $obj->fetchAll()->toArray();
        $arr = array();
        foreach($row as $r) $arr[$r['CTC_ID']] = $r['CTC_NOM'];
        
        $this->view->submenu_page = TRUE;
        
        $id = $this->_getParam('id',0);
        
        if($id > 0){
            
            $obj = new Ead_Model_DbTable_Material();
            $row = $obj->fetchRow('MAT_ID = ' . $id);
            $rep = $row->findDependentRowSet("Ead_Model_DbTable_Repositorio"); 

            $aquivoNome = md5($rep[0]['REP_ID'].$rep[0]['REP_DATHOR']).'.*';

            $arquivo = 'public/upload/repositorio/'.$aquivoNome; // caminho absoluto do arquivo
            foreach (glob($arquivo) as $src) {
                $arquivo = $src;
            }

            if (!file_exists($arquivo)) {
                exit;
            }

            $obj = new Ead_Model_DbTable_Material();
            $row = $obj->fetchRow();

            $ext = end(explode('.', $arquivo));
            $novoNome = $row['MAT_NOM'].'.'.$ext;


            header('Content-Description: File Transfer');
            header('Content-Disposition: attachment; filename="'.$novoNome.'"');
            header('Content-Type: application/octet-stream');
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: ' . filesize($arquivo));
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Expires: 0');

            readfile($arquivo);
        }
    }
    
    public function filtroAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        $this->view->limitRegistros = $this->limitRegistros;
        
        $where = 'MAT_STA = 1';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where .= ' and CTC_ID like "%'.$filtro.'%"';
        
        $obj = new Ead_Model_DbTable_Material();
        $result = $obj->fetchAll($obj->select()->where($where)->order(array('MAT_ORD ASC','MAT_NOM DESC'))->limit($this->limitRegistros,0));
        
        $count = 0;
        $btn_mais = 1;
        $total = $obj->fetchAll($where);
        $count = count($total);
        if($count == 0){
            $offset = 0;
            $btn_mais = 0;
        } else if($count <= $this->limitRegistros) {
            $btn_mais = 0;
        }
        
        $html =  $this->view->partial('materiais/list-materias.phtml', array('urlbase' => $this->_urlbase,'controller' => $this->_controller,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'btn_mais'=>$btn_mais));
    }
    
    public function downloadAction(){
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        
        if($row->MAT_CENE == 1 && Zend_Auth::getInstance()->getStorage()->read()->USU_CENE == 0){
            $this->_helper->redirector('index', $this->_controller,array());
        }
        
        $id = $this->_getParam('id');
        
        $this->verificaLogin('index'); 
        
        Zend_Loader::loadClass('estatisticas','library');
        $obj = new Estatisticas();
        $obj->acesso_pagina($id,2); 
        
        $obj = new Ead_Model_DbTable_Material();
        $row = $obj->fetchRow('MAT_ID = ' . $id);
        $rep = $row->findDependentRowSet("Ead_Model_DbTable_Repositorio"); 
        
        $aquivoNome = md5($rep[0]['REP_ID'].$rep[0]['REP_DATHOR']).'.*';
        
        $arquivo = 'public/upload/repositorio/'.$aquivoNome; // caminho absoluto do arquivo
        foreach (glob($arquivo) as $src) {
            $arquivo = $src;
        }

        if (!file_exists($arquivo)) {
            exit;
        }
       
        $ext = end(explode('.', $arquivo));
        $novoNome = $row['MAT_NOM'].'.'.$ext;
      
        
        header('Content-Description: File Transfer');
        header('Content-Disposition: attachment; filename="'.$novoNome.'"');
        header('Content-Type: application/octet-stream');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . filesize($arquivo));
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Expires: 0');

        readfile($arquivo);
    }
    
    public function maismateriaisAction(){
        
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender(); 
        $this->view->limitRegistros = $this->limitRegistros;
        
        $offset = (int)$this->_getParam('offset', 0) + $this->limitRegistros;
        
        $where = 'MAT_STA = 1';
        $filtro = (int)$this->_getParam('filtro', 0);
        if($filtro > 0) $where .= ' and CTC_ID like "%'.$filtro.'%"';
        
        $obj = new Ead_Model_DbTable_Material();
        $result = $obj->fetchAll($obj->select()->where($where)->order(array('MAT_ORD ASC','MAT_NOM DESC'))->limit($this->limitRegistros,$offset));
        
        $count = 0;
        $btn_mais = 1;
        $total = $obj->fetchAll($where);
        $count = count($total);
        if($count == 0){
            $offset = $offset - $this->limitRegistros;
            $btn_mais = 0;
        }
        else if(($count - $offset) <= $this->limitRegistros) {
            $btn_mais = 0;
        }
            
        $html =  $this->view->partial('materiais/list-materias.phtml', array('urlbase' => $this->_urlbase,'controller' => $this->_controller,'result'=>$result));
        
        echo json_encode(array('ok' => true, 'html' => $html,'offset'=>$offset,'btn_mais'=>$btn_mais));
        
    }
}

